package com.jandan.persistence.sqlmapdao;

import java.sql.SQLException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.jandan.persistence.iface.AccountDao;
import com.jandan.ui.model.Account;

/**
 * AccountDao接口实现类，使用Ibatis框架进行持久化
 * @author Gong Yong
 * @version 1.0
 * @see com.jandan.persistence.iface.AccountDao
 */
public class AccountSqlMapDao extends SqlMapClientDaoSupport implements AccountDao {
	public List<Account> getAllAccountList() {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("Account.getAllAccountList");
	}

	public List<Account> getAllAccountList(int start, int limit) {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("Account.getAllAccountList",start,limit);
	}
	
	public Account getAccountByUserID(long userID) {
		// TODO Auto-generated method stub
		return (Account)this.getSqlMapClientTemplate().queryForObject("Account.getAccountByUserID",Long.valueOf(userID));
	}


	public Account getAccountByUserName(String userName) {
		// TODO Auto-generated method stub
		return (Account)this.getSqlMapClientTemplate().queryForObject("Account.getAccountByUserName",userName);
	}


	public Account getAccountByUserNameAndPassword(String userName,
			String password) {
		// TODO Auto-generated method stub
		Map<String,String> map = new HashMap<String,String>();
		map.put("userName",userName);
		map.put("password",password);
		return (Account)this.getSqlMapClientTemplate().queryForObject("Account.getAccountByUserNameAndPassword",map);
	}

	
	public long insertAccount(Account account) {		
		// TODO Auto-generated method stub
		long accountID = 0;
		try {
			this.getSqlMapClient().startTransaction();
			getSqlMapClientTemplate().update("Account.insertAccount", account);
			accountID = (Long)getSqlMapClientTemplate().queryForObject("Account.lastInsertAccountID");
			this.getSqlMapClient().commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				this.getSqlMapClient().endTransaction();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return accountID;
	}

	
	public void updateAccount(Account account) {
		// TODO Auto-generated method stub
		getSqlMapClientTemplate().update("Account.updateAccount", account);
	}
	
	
	public void deleteAccount(long userID) {
		// TODO Auto-generated method stub
		getSqlMapClientTemplate().delete("Account.deleteAccount", Long.valueOf(userID));
	}

	public int getTotalAccountCount() {
		// TODO Auto-generated method stub
		return (Integer)getSqlMapClientTemplate().queryForObject("Account.getTotalAccountCount");
	}

}
